/**
 * @Title: LeaveOutAdapter.java
 * @Package: com.jason.bluetooth.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月9日23:32:04
 * @Version: V1.0
 */
package com.jason.platform.supervision.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.activity.BaiduMapActivity;
import com.jason.platform.supervision.activity.LocateActivity;
import com.jason.platform.supervision.beans.LocateBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LocateAdapter
 * @Description: 人员定位
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-8-15 14:57:02
 */
public class LocateAdapter extends BaseAdapter {

    private static final String TAG = LocateAdapter.class.getSimpleName();

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<LocateBean> mDatas = new ArrayList<LocateBean>();

    public LocateAdapter(Context context) {
        super();
        this.mContext = context;
        this.mLayoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mDatas = new ArrayList<LocateBean>();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addAll(List<LocateBean> datas) {
        this.mDatas = datas;
        if (mDatas == null) {
            mDatas = new ArrayList<LocateBean>();
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(
                    R.layout.adapter_locate, null);
            holder = new Holder();
            holder.mNameTextView = (TextView) convertView
                    .findViewById(R.id.locate_name);
            holder.mPhoneTextView = (TextView) convertView
                    .findViewById(R.id.locate_phone);
            holder.mDateTextView = (TextView) convertView
                    .findViewById(R.id.locate_date);
            holder.mStateTextView = (TextView) convertView
                    .findViewById(R.id.locate_state);
            holder.mTimeTextView = (TextView) convertView
                    .findViewById(R.id.locate_time);
            holder.mOperateTextView = (TextView) convertView
                    .findViewById(R.id.locate_operate);
            holder.mLocateTextView = (TextView) convertView.findViewById(R.id.locate_locate);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        final LocateBean bean = (LocateBean) getItem(position);
        if (bean != null) {
            Log.i(TAG, "******position:" + position + "*******" + bean.toString());

            holder.mNameTextView.setText(bean.getElectronicsName());
            holder.mPhoneTextView.setText(bean.getTelephone());
            holder.mStateTextView.setText(bean.getState());
            String date = bean.getPositionTime();
            if (!TextUtils.isEmpty(date)) {
                holder.mDateTextView.setText(date.split(" ")[0]);
                holder.mTimeTextView.setText(date.split(" ")[1]);
            }
            holder.mStateTextView.setText(bean.getState());
            holder.mOperateTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 打开地图

                    if (bean.isOpenMap()) {
                        Intent intent = new Intent(mContext, BaiduMapActivity.class);
                        intent.setAction(BaiduMapActivity.ACTION_LOCATE_LOOK);
                        intent.putExtra(BaiduMapActivity.KEY_LOCATE_LOOK, bean);
                        mContext.startActivity(intent);
                    } else {
                        Toast.makeText(mContext, "暂无坐标信息", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            holder.mLocateTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!LocateActivity.isLoading){
                        LocateActivity.isLoading = true;
                        ((LocateActivity)mContext).getLocate(bean);
                    }else {
                        Toast.makeText(mContext,"请稍等，正在获取位置信息...",Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }

        //TODO 填充数据
        return convertView;
    }

    class Holder {
        TextView mNameTextView;
        TextView mPhoneTextView;
        TextView mDateTextView;
        TextView mTimeTextView;
        TextView mStateTextView;
        TextView mOperateTextView;
        TextView mLocateTextView;

    }

}
